<?php
	use\ampleharvest\code\cConfig;
	use\ampleharvest\code\cRestServices;
	use\ampleharvest\code\cRestConnector;
	require_once dirname(dirname(dirname(__FILE__))) . '/config/cConfig.php';
	require_once dirname(dirname(dirname(__FILE__))) . "/config/cRestServices.php";

	require_once dirname(dirname(dirname(__FILE__))) . '/classes/Tables/cTABLE_Data.php';
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/cRestConnector.php";

	require_once dirname(dirname(dirname(__FILE__))) . '/classes/cAmpleHarvestLog.php';

	require_once dirname(dirname(dirname(__FILE__))) . '/classes/cStomp.php';
	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 02 JUN 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code
															
	 */
	set_error_handler('exceptions_error_handler');

	function exceptions_error_handler($severity, $message, $filename, $lineno)
	{
		if (error_reporting() == 0)
		{
			return;
		}
		if (error_reporting() & $severity)
		{
			throw new ErrorException($message, 0, $severity, $filename, $lineno);
		}
	}

	//Number of Data sets to process in one pass.
	$int_GroupSize = 100;

	$arr_DataSets = array();

	$obj_Log = new cAmpleHarvestLog();

	//Setup Stomp
	//$obj_StompCon = new cStomp();

	$obj_RestCon = new cRestConnector(cRestServices::get_AHDataService());
	$obj_RestCon->set_Login(cConfig::C_STR_AMPLE_SILO_USER_NAME, cConfig::C_STR_AMPLE_SILO_PASSWORD);

	$obj_TABLE_Data = new cTABLE_Data();

	//Search for all Rows that have a current status of New
	$obj_TABLE_Data->start_Group();

	$obj_TABLE_Data->add_ColumnQuery($obj_TABLE_Data->m_int_COL_Status, cTABLE_Data::C_INT_STATUS_INDEX_SUCCESS);

	$obj_TABLE_Data->end_Group();

	$obj_TABLE_Data->set_Limit($int_GroupSize);

	//Do Query
	$int_Items = $obj_TABLE_Data->do_Search();
	print("Processing " . $int_Items . " rows of data\n");

	$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_DATA_PROC_1_INFO,
		"Moveing $int_Items itmes to HDFS.",
		cAmpleHarvestLog::C_INT_LEVEL_INFO);

	if ($int_Items < 1)
	{
		exit(0);
	}

	$arr_Results = $obj_TABLE_Data->get_ResultSet();

	//print_r($arr_DataSets);

	foreach ($arr_Results as $arr_Result)
	{
		$obj_TABLE_Data->clear();
		$obj_TABLE_Data->load_DataFromArray($arr_Result);
		$arr_DataSets[] = $arr_Result;

		$obj_TABLE_Data->set_data();

		$obj_TABLE_Data->set_status(5);

		$obj_TABLE_Data->save_Data2DB();
	}

	foreach ($arr_DataSets as $arr_DataSet)
	{
		$bool_Save = true;

		$arr_Data = array();
		$obj_TABLE_Data->clear();
		$obj_TABLE_Data->load_DataFromArray($arr_DataSet);

		$str_Json = \cTABLE_Data::decode($obj_TABLE_Data->get_data());

		try
		{
			$obj_Json = json_decode($str_Json);
		}
		catch (Exception $e)
		{
			print($obj_TABLE_Data->get_id() . "Failed\n");
			continue;
		}

		if (!isset($obj_Json->id))
		{
			print($obj_TABLE_Data->get_id() . " - No Json ID\n");
			continue;
		}

		$obj_RestCon->clear_Params();
		$obj_RestCon->set_Param("id", $obj_Json->id);
		$obj_RestCon->set_Param("origId", $obj_Json->origId);
		$obj_RestCon->set_Param("content", $str_Json);

		print("id - " . $obj_Json->id . "\n");
		print("URL - " . cRestServices::get_AHDataService() . "\n");

		print($obj_RestCon->send_Put() . "\n");
		$obj_DataSet->set_status(6);
		if (!$bool_Save)
		{
			print("Error Sending Data\n");

			$obj_DataSet->set_status(-3);
		}
		else
		{
			$obj_DataSet->set_status(6);
		}
		if (!$obj_DataSet->save_Data2DB())
		{
			$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_DATA_PROC_1_ERROR,
				$obj_DataSet->get_id() . " had an error updateing its information in the database.",
				cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
			print("Error Writeing to Database\n");
		}
	}

	function validate_Row(cTABLE_Data $obj_Data, $obj_Json)
	{
		global $obj_Log;
		if ($obj_Data->get_id() != $obj_Json->id)
		{
			$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_DATA_PROC_1_ERROR,
				$obj_Data->get_id() . " has miss matching IDs the row id and the Json ID must match",
				cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
			return false;
		}
		return true;
	}

	exit(1);
?>